package com.mes.web.controller.cb;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mes.entity.cb.Pages;
import com.mes.entity.cb.WarehRerdMx;
import com.mes.entity.cb.WarehouRed;
import com.mes.entity.tdd.ProcurementRukuMateriel;
import com.mes.service.cb.WarehouRedService;
import com.mes.utils.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: WarehouRedController
 * @Description: 入库记录
 * @Author cb
 * @Date 2018年4月10日15:51:04
 * @version 1.0
 */
@Controller
@RequestMapping("/ware")
public class WarehouRedController {


    @Resource
    private WarehouRedService warehouRedService;

    /**
     * list分页
     * @param response
     * @param limit
     * @param offset
     * @throws Exception
     */
    @RequestMapping("/ware_list")
    public void ware_list(HttpServletResponse response, String limit, String offset,String danHao) throws Exception {

        Pages pages = CustomUtils.pagesql(limit, offset);

        if (StringUtil.isNotEmpty(danHao)) {
            pages.setNumber(danHao);
        }

        List<WarehouRed> list = warehouRedService.ware_list(pages);
        int toatal = warehouRedService.getTotal(pages);

        JSONObject jsonObject = new JSONObject();
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
        jsonObject.put("total", toatal);
        jsonObject.put("rows", jsonArray);
        ResponseUtil.write(response, jsonObject);


    }

    /**
     * 入库详情
     * @param response
     * @param limit 页面大小
     * @param offset 当前页
     */
    @RequestMapping("/wareMx_list")
    public void wareMx_list(HttpServletResponse response, String limit, String offset,long rwid) throws Exception {


        Pages pages = CustomUtils.pagesql(limit, offset);
        pages.setRid(rwid);

        List<WarehRerdMx> list = warehouRedService.wareMX_list(pages);
        int toatal = warehouRedService.getTotalMx(pages);


        JSONObject jsonObject = new JSONObject();
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
        jsonObject.put("total", toatal);
        jsonObject.put("rows", jsonArray);
        ResponseUtil.write(response, jsonObject);
    }



    /**
     * pda扫码
     * @param oddNumId 入库单号
     * @param userId 用户id
     * @param procurementRukuMaterielList jsonArray 解析
     */
    @RequestMapping("/ware_insertPhone")
    @ResponseBody
    public  Map<String,Object> ware_insertPhone(String oddNumId , String userId,String procurementRukuMaterielList,HttpServletResponse response) throws Exception {

        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyMMddHHmmss");



        List<ProcurementRukuMateriel> list = getListPersonByGson(procurementRukuMaterielList);

        //入库
        WarehouRed warehouRed = new WarehouRed();

        //操作人
        warehouRed.setRman(Long.parseLong(userId));
        //入库单
        warehouRed.setRed(simpleDateFormat.format(new Date()));

        //入库单添加
        long rid = warehouRedService.ware_insert(warehouRed);

        List<WarehRerdMx> warehRerdMxes = new ArrayList<>();

        for (ProcurementRukuMateriel pm : list) {

            WarehRerdMx warehRerdMx = new WarehRerdMx();
            //物料id
            warehRerdMx.setMaId(pm.getMaterielid());
            //入库单id
            warehRerdMx.setRwId(rid);
            //条码
            warehRerdMx.setCode(pm.getBarcode());
            //批号
            warehRerdMx.setPihao(pm.getPmbatchnumber());
            //数量
            warehRerdMx.setNum(Long.parseLong(pm.getNumber()));
            //操作人
            warehRerdMx.setUserId(Long.parseLong(userId));

            warehRerdMxes.add(warehRerdMx);

            warehRerdMx = null;
        }

        //入库单详细添加
        Integer affect = warehouRedService.wareMX_insert(warehRerdMxes);

        Map<String,Object> amp = new HashMap<>();

        if(affect > 0) {
        	
        	warehouRedService.updateBymaRuKu(list.get(0).getPrid());

            amp.put("code","200");
            amp.put("message","入库成功");

        }else {
            amp.put("code","500");
            amp.put("message","入库失败");
        }

        return amp;

    }

    /**
     * 解析jsonArray
     * @param procurementRukuMaterielList
     */
    private List<ProcurementRukuMateriel> getListPersonByGson(String procurementRukuMaterielList) {
        List<ProcurementRukuMateriel> list = new ArrayList<>();
        Gson gson = new Gson();

        list = gson.fromJson(procurementRukuMaterielList, new TypeToken<List<ProcurementRukuMateriel>>(){}.getType());

        return list;
    }
}
